// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package edu.uwlax.cs.oayonlinestore.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import edu.uwlax.cs.oayonlinestore.vo.OrderEditVO;
import edu.uwlax.cs.oayonlinestore.vo.OrderExtendedVO;
import edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO;

/**
 * @see edu.uwlax.cs.oayonlinestore.entities.Order
 */
public class OrderDaoImpl extends
		edu.uwlax.cs.oayonlinestore.entities.OrderDaoBase {

	/**
	 * Retrieves the entity object that is associated with the specified value
	 * object from the object store. If no such entity object exists in the
	 * object store, a new, blank entity is created
	 */
	private edu.uwlax.cs.oayonlinestore.entities.Order loadOrderFromOrderEditVO(
			edu.uwlax.cs.oayonlinestore.vo.OrderEditVO orderEditVO) {

		edu.uwlax.cs.oayonlinestore.entities.Order order = this
				.load(orderEditVO.getId());
		if (order == null) {
			order = edu.uwlax.cs.oayonlinestore.entities.Order.Factory
					.newInstance();
		}
		return order;

	}

	/**
	 * Retrieves the entity object that is associated with the specified value
	 * object from the object store. If no such entity object exists in the
	 * object store, a new, blank entity is created
	 */
	private edu.uwlax.cs.oayonlinestore.entities.Order loadOrderFromOrderSummaryVO(
			edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO orderSummaryVO) {

		edu.uwlax.cs.oayonlinestore.entities.Order order = this
				.load(orderSummaryVO.getId());
		if (order == null) {
			order = edu.uwlax.cs.oayonlinestore.entities.Order.Factory
					.newInstance();
		}
		return order;

	}

	/**
	 * @see edu.uwlax.cs.oayonlinestore.entities.OrderDao#orderEditVOToEntity(edu.uwlax.cs.oayonlinestore.vo.OrderEditVO)
	 */
	public Order orderEditVOToEntity(OrderEditVO orderEditVO) {
		Order entity = this.loadOrderFromOrderEditVO(orderEditVO);
		this.orderEditVOToEntity(orderEditVO, entity, true);
		return entity;
	}

	public Order orderExtendedVOToEntity(OrderExtendedVO orderExtendedVO) {
		Order target = loadOrderFromOrderSummaryVO(orderExtendedVO);
		orderExtendedVOToEntity(orderExtendedVO, target, true);
		return target;
	}

	/**
	 * @see edu.uwlax.cs.oayonlinestore.entities.OrderDao#orderSummaryVOToEntity(edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO)
	 */
	public edu.uwlax.cs.oayonlinestore.entities.Order orderSummaryVOToEntity(
			edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO orderSummaryVO) {
		// @todo verify behavior of orderSummaryVOToEntity
		edu.uwlax.cs.oayonlinestore.entities.Order entity = this
				.loadOrderFromOrderSummaryVO(orderSummaryVO);
		this.orderSummaryVOToEntity(orderSummaryVO, entity, true);
		return entity;
	}

	/**
	 * @see edu.uwlax.cs.oayonlinestore.entities.OrderDao#toOrderEditVO(edu.uwlax.cs.oayonlinestore.entities.Order,
	 *      edu.uwlax.cs.oayonlinestore.vo.OrderEditVO)
	 */
	public void toOrderEditVO(
			edu.uwlax.cs.oayonlinestore.entities.Order source,
			edu.uwlax.cs.oayonlinestore.vo.OrderEditVO target) {
		super.toOrderEditVO(source, target);
		target.setStatus(source.getStatus().getValue());
		if (source.getCcExpirationDate() != null)
			target.setCcExpirationDate(new Date(source.getCcExpirationDate()
					.getTime()));
	}

	public void toOrderExtendedVO(Order source, OrderExtendedVO target) {
		toOrderSummaryVO(source, target);

		Collection orderItems = source.getItems();
		getOrderItemDao().toOrderItemVOCollection(orderItems);
		target.setOrderItems(new ArrayList(orderItems));

		Collection giftCards = source.getGiftCards();
		getGiftCardDao().toGiftCardVOCollection(giftCards);
		target.setGiftCards(new ArrayList(giftCards));
	}

	public void toOrderSummaryVO(Order source, OrderSummaryVO target) {
		super.toOrderSummaryVO(source, target);

		final float defTax = 5.5f; // TODO: load tax from somewhere else
		final float defShip = 3f; // TODO: load shipping from somewhere else

		target.setShipping(source.calcShipping(defShip));
		target.setTaxMoney(source.calcTotalTax(defTax));
		target.setTaxPercent(defTax);
		target.setTotal(source.calcTotalPrice(defTax, defShip));

		target.setStatus(source.getStatus().getValue());
		target.setUser(getUserDao().toUserVO(source.getUser()));
		target.setItemCount(source.calcItemCount());

		if (source.getShippingDate() != null)
			target
					.setShippingDate(new Date(source.getShippingDate()
							.getTime()));

		if (source.getOrderDate() != null)
			target.setOrderDate(new Date(source.getOrderDate().getTime()));
	}

}